<?php

namespace app\api\controller;

use app\api\model\Activity;
use app\api\model\Banner;
use app\api\model\Goods;
use app\api\model\Player;
use app\api\model\VoteLog;
use app\common\controller\Api;
use EasyWeChat\Factory;
use think\Cache;
use think\Config;
use think\Hook;

/**
 * 首页接口【这里】
 */
class Index extends Api
{
    protected $noNeedLogin = ['activity', 'playerPage', 'playerIndexPage', 'getPlayer', 'getWishPage', 'getPlayerPosters', 'getActivityPosters', 'getWishPage'];
    protected $noNeedRight = ['*'];


    /**
     * 活动详情
     * @param int id 活动id
     */
    public function activity()
    {
        $id = $this->request->param('id');
        $ret = Activity::get($id);
        Activity::where("id", $id)->setInc("browse");
        $banner = Banner::where("activity_id", $id)->order("weigh", "desc")->select();
        $ret['banner'] = $banner;
        $this->success($ret);
    }

    /**
     * 排行榜
     * @param int id 活动id
     *
     */
    public function playerPage()
    {
        $id = $this->request->param('id');
        $so = $this->request->param('so');
        $model = Player::where('activity_id', $id);
        if (!empty($so)) {
            $model->where(function ($query) use ($so) {
                $query->whereLike('name', "%" . $so . "%")
                    ->whereOr('no', $so);
            });
        }
        $ret = $model->order("vote_number", "desc")->paginate();
        $this->success("", $ret);
    }


    /**
     * 选手列表
     * @param int id 活动id
     *
     */
    public function playerIndexPage()
    {
        $id = $this->request->param('id');
        $so = $this->request->param('so');
        $model = Player::where('activity_id', $id);
        if (!empty($so)) {
            $model->where(function ($query) use ($so) {
                $query->whereLike('name', "%" . $so . "%")
                    ->whereOr('no', $so);
            });
        }
        $ret = $model->orderRaw("rand()")->paginate();
        $this->success("", $ret);
    }

    /**
     * 选手报名
     * @ApiMethod (POST)
     * @param string name 名称
     * @param string image 头像
     * @param string phone 手机号
     * @param string describe 描述
     * @param int activity_id 活动id
     * @param string souvenir_date 纪念日
     * @param string wonderful_moment 精彩瞬间
     * @param string oath 打榜誓言
     */
    public function apply()
    {
        $params = $this->request->post();
        $userId = $this->auth->getUser()['id'];
        $model = new Player();

        $player = $model->where('user_id', $userId)->where("activity_id", $params['activity_id'])->find();
        if (!empty($player)) {
            $this->error("已报名，无需重复报名");
        }
        $time = time();
        $activity = Activity::where("id", $params['activity_id'])
            ->where("vote_begin_time", '<=', $time)
            ->where("vote_end_time", ">=", $time)
            ->find();
        if (empty($activity)) {
            $this->error("报名已结束");
        }
        $model->save([
            'name' => $params['name'],
            'image' => $params['image'],
            'phone' => $params['phone'],
            'activity_id' => $params['activity_id'],
            'user_id' => $userId,
            'vote_number' => 0,
            'describe' => $params['describe'],
            'no' => $activity['apply'] + 1,
            'souvenir_date' => $params['souvenir_date'],
            'wonderful_moment' => $params['wonderful_moment'],
            'oath' => $params['oath']
        ]);
        $activity->setInc('apply');
        $this->success("");
    }

    /**
     * 报名详情
     * @param int activity_id 活动id
     */
    public function applyInfo()
    {
        $activity_id = $this->request->param("activity_id");
        $userId = $this->auth->id ?? 0;
        $model = new Player();
        $player = $model->where('user_id', $userId)->where("activity_id", $activity_id)->find();
        $this->success("", $player);
    }

    /**
     * 选手详情
     * @param int id 选手id
     */
    public function getPlayer()
    {
        $id = $this->request->param("id");
        $ret = Player::get($id);
        $this->success('', $ret);
    }


    /**
     * 投票
     * @ApiMethod (POST)
     * @param int activity_id 活动id
     * @param int player_id 选手id
     */
    public function votePlayer()
    {
        $params = $this->request->post();
        $userId = $this->auth->getUser()['id'];

        $cache = Cache::get("vote_" . $userId, 0);
        if ($cache != 0) {
            $this->error("已投票");
        }
        Cache::set("vote_" . $userId, 1, 60);
        $log = VoteLog::where("activity_id", $params['activity_id'])->where("user_id", $userId)->find();
        if (!empty($log)) {
            $this->error("已投票");
        }
        Activity::where("id", $params['activity_id'])->setInc("vote_number");
        Player::where("id", $params['player_id'])->setInc("vote_number");
        $model = new VoteLog();
        $model->save([
            'activity_id' => $params['activity_id'],
            'user_id' => $userId,
            'player_id' => $params['player_id'],
            'vote_time' => time()
        ]);
        $this->success("投票成功");
    }


    /**
     * 获取选手海报
     * @param int id 选手id
     */
    public function getPlayerPosters()
    {
        $id = $this->request->param('id');
        $player = Player::get($id);
        $activity = Activity::get($player['activity_id']);
        $params = [
            'id' => $activity['player_posters_id'],
            'params' => [
                'qr_2' => [
                    'player_id' => $player['id']
                ],
                'text_1' => [
                    'oath' => $player['oath'],
                ],
                'text_5' => [
                    'name' => $player['name']
                ],
                'image_4' => cdnurl($player['image']),
            ],
            'output' => true,
            'size' => 1.5
        ];
        Hook::listen('posters', $params, null, true);
    }

    /**
     * 获取活动海报
     * @param int id 活动id
     */
    public function getActivityPosters()
    {
        $id = $this->request->param('id');
        $activity = Activity::get($id);
        $params = [
            'id' => $activity['posters_id'],
            'params' => [
                'qr_2' => [
                    'id' => $id,
                ]
            ],
            'output' => true,
            'size' => 1.5
        ];
        Hook::listen('posters', $params, null, true);
    }

    /**
     * 祝福分页
     * @param int id 选手id/活动id
     * @param int type 类型【1=活动,2=选手】
     */
    public function getWishPage()
    {
        $id = $this->request->param('id');
        $type = $this->request->param('type', 1);
        if ($type == 1) {
            $page = \app\api\model\Order::where("activity_id", $id)->where("status", "1")->orderRaw("rand()")->paginate();
        } else {
            $page = \app\api\model\Order::where("player_id", $id)->where("status", "1")->orderRaw("rand()")->paginate();
        }
        $this->success("", $page);
    }

    /**
     * 商品列表
     * @param int activity_id 活动id
     */
    public function goodsList()
    {
        $activity_id = $this->request->param("activity_id");
        $goods = Goods::where('activity_id', $activity_id)->order("weigh", "desc")->select();
        $this->success("", $goods);
    }

}
